package com.saltedfish.tool.handler;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.saltedfish.tool.service.AlarmService;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import cn.droidlover.xdroidmvp.log.XLog;

public class AlarmTimerUtil {

    private static final int INTERVAL = 1000 * 60 * 60 * 24;// 24h

    private static final String ACTION_NAME = "android.intent.action.alarm.timer";// 广播名称

    /**
     *
     * 设置定时闹钟
     *
     * @param context
     * @param alarmId
     * @param action
     * @param map 要传递的参数
     */
    public static void setAlarmTimer(Context context, int alarmId, String time, String action, Map<String, Serializable> map) {
//        XLog.e("time--->>"+time);

        Intent myIntent = new Intent();
        myIntent.setAction(action);
        if(map != null){
            for (String key: map.keySet()) {
                myIntent.putExtra(key,map.get(key));
            }
        }

        //context.sendBroadcast(myIntent);

//        PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, myIntent, 0);//如果是广播，就这么写
        PendingIntent sender = PendingIntent.getService(context, alarmId, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Calendar instance = Calendar.getInstance();

        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = null;
        try {
            currentDate = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        XLog.e("currentDate--->>"+currentDate);

        instance.setTime(currentDate);

//        XLog.e("instance.getTime---->>"+instance.getTime());


       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(), INTERVAL,sender);//这里一定要注意，一定要相对时间对应相对时间，
//

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //MARSHMALLOW OR ABOVE
//            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(), sender);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //LOLLIPOP 21 OR ABOVE
//            AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(instance.getTimeInMillis(), sender);
//            alarmManager.setAlarmClock(alarmClockInfo, sender);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //KITKAT 19 OR ABOVE
//            alarmManager.setExact(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(), sender);
//        } else { //FOR BELOW KITKAT ALL DEVICES
//            alarmManager.set(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(), sender);
//        }
    }

    /**
     * 取消闹钟
     * @param context
     * @param action
     */
    public static void cancelAlarmTimer(Context context, String action,int alarmId) {
        Intent myIntent = new Intent();
        myIntent.setAction(action);
//        PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, myIntent, 0);//如果是广播，就这么写
        PendingIntent sender = PendingIntent.getService(context, alarmId, myIntent,0);
        AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(sender);
    }

}
